//发送Ajax请求
import config from './config'
export default (url,data={},method) => {
    return new Promise((resolve,reject) =>
    // 1.new Promise初始化Promise实例的状态为pending
        wx.request({
            url:config.host +  url,
            // url:config.mobileHost  +  url,
            data,
            method,
            header:{
                cookie:wx.getStorageSync('cookies')?wx.getStorageSync('cookies').find(item=>item.indexOf('JSESSIONID')!==-1):''
            },
            success: (res) =>{
                if (data.isLogin) {
                    //登录请求
                    //将用户的cookie存入本地
                    wx.setStorage({
                        key:'cookies',
                        data:res.cookies
                    })
                    console.log(wx.getStorageSync('cookies').find(item=>item.indexOf('JSESSIONID')!==-1))
                }
                resolve(res.data);//修改Promise的状态为成功状态resolved
            },
            fail: (err)=>{
            console.log(err);
            reject(err)//修改Promise的状态为失败状态rejected
            }
        })
      )
}